﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Sharp.Domain.PlatformBoundedContext.PersonModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.UserModule.Aggregates;
using Sharp.Domain.Repositories;
using Sharp.Domain.Repository.EF;
using Sharp.Infrastructure;

namespace Sharp.Domain.PlatformBoundedContext.PersonModule.Services
{
    public partial class PersonService : IPersonService
    {
        private IRepositoryContext _repositoryContext;
        private IEmployeeRepository _employeeRepository;
        private IJobRepository _jobRepository;

        public PersonService(IRepositoryContext repositoryContext,
            IEmployeeRepository employeeRepository,
            IJobRepository jobRepository)
        {
            _repositoryContext = repositoryContext;
            _employeeRepository = employeeRepository;
            _jobRepository = jobRepository;
        }

        /// <summary>
        /// 设置为用户
        /// </summary>
        /// <param name="user"></param>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public Result EmployeeAsUser(User user, int employeeId)
        {
            var res = new Result();
            var ef = _employeeRepository.Context as IEntityFrameworkRepositoryContext;
            ef.Context.Configuration.AutoDetectChangesEnabled = true;
            ef.Context.Configuration.ProxyCreationEnabled = true;
            ef.Context.Configuration.LazyLoadingEnabled = true;

            var employee = _employeeRepository.Get(x => x.Id == employeeId, x => x.User);
            if (employee.User != null && employee.User.EntityState != Model.EntityStateOperate.IsDraft)
            {
                res.Err = "已经是本系统的用户了。";
                return res;
            }

            if (user.UserName.IsNullOrEmpty())
            {
                res.Err = "用户名不能为空。";
                return res;
            }
            if (user.Password.IsNullOrEmpty())
            {
                res.Err = "密码不能为空。";
                return res;
            }

            if (_employeeRepository.Exists(x => x.User.UserName == user.UserName))
            {
                res.Err = "用户名已经存在。";
                return res;
            }
            if (employee.User == null)//用户表没数据
            {
                ef.ExecuteCommand(@"INSERT INTO Platform_Users (Id,UserName,Password,Email,EntityState,LastLogonTime,LastLogonIp,CreatedTime,CreatedUserId,ModifiTime,ModifiedUserId,Face,PhoneNember) VALUES (@p0, @p1, @p2, '', 1, null, '', @p3, @p4, null, null, '', '')", employee.Id, user.UserName,
                    user.Password.Sha1(), DateTime.Now, HttpContext.Current.User.Identity.GetUserId());
            }
            else//用户表有数据
            {
                employee.User.UserName = user.UserName;
                employee.User.Password = user.Password.Sha1();
                employee.User.EntityState = Model.EntityStateOperate.Normal;
                employee.User.ModifiTime = DateTime.Now;
                employee.User.ModifiedUserId = HttpContext.Current.User.Identity.GetUserId();
                _employeeRepository.Update(employee);
            }
            _employeeRepository.Context.Commit();

            return res;
        }

        public Result DeleteEmployee(int[] ids)
        {
            _employeeRepository.Remove(ids);

            _repositoryContext.Commit();
            return new Result();
        }

        public Result DeleteJob(int[] ids)
        {
            _jobRepository.Remove(ids);

            _repositoryContext.Commit();
            return new Result();
        }


    }
}